import {useCallback, useState} from 'react';
import {DialogOperate} from "@/global";

interface DialogStatus {
  /** 显示状态 */
  visible: boolean;
  /** 参数对象 */
  params: { [key: string]: any };
}

/** 对话框操作组件 */
function useDialog(): DialogOperate {
  const [status, setStatus] = useState<DialogStatus>({visible: false, params: {}});

  /** 关闭窗口函数 */

  /** 设置对话框可见状态 */
  const setVisible = useCallback((_visible: boolean, params?: any) => {
    setStatus({visible: _visible, params: params || {}});
  }, []);

  /** 显示对话框 */
  const show = useCallback((params?: any) => {
    setStatus({visible: true, params: params || {}});
  }, []);

  /** 隐藏对话框 */
  const hide = useCallback(() => {
    setStatus({visible: false, params: {}});
  }, []);

  /** 隐藏对话框 */
  const hideCallback = useCallback((callback?: () => void) => {
    setStatus({visible: false, params: {}});
    callback && callback();
  }, []);

  return {
    ...status,
    setVisible,
    show,
    hide,
    hideCallback,
  };
}

export default useDialog;

